import { _decorator, Component, EventTouch, Node, UITransform, v2, v3, Vec2 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass
export default class BezierMovement extends Component {
    @property // 移动速度
    speed: number = 1;

    public curvePoints: Vec2[] = []; // 贝塞尔曲线上的点
    private currentIndex: number = 0; // 当前移动到的点索引

    private canMove = false;

    public startMove(points: Vec2[]) {
        this.canMove = true;
        this.curvePoints = points;
        // 初始化物体位置
        if (this.curvePoints.length > 0) {
            this.node.position = v3(this.curvePoints[0].x, this.curvePoints[0].y);
        }
    }

    update(dt: number) {
        if (!this.canMove && this.curvePoints.length === 0) return;

        // 计算移动进度
        this.currentIndex += this.speed * dt;

        // 如果超出范围，则回到起点
        if (this.currentIndex >= this.curvePoints.length) {
            this.currentIndex = 0;
        }

        // 更新物体位置
        const pointIndex = Math.floor(this.currentIndex);
        if (pointIndex < this.curvePoints.length) {
            this.node.position = v3(this.curvePoints[pointIndex].x, this.curvePoints[pointIndex].y);
        }
    }
}